namespace imitateMacQQ.WebApi.Configures;

public class NLogHelper : INLogHelper
{
    private readonly IHttpContextAccessor _httpContextAccessor;

    private readonly ILogger<NLogHelper> _logger;

    public NLogHelper(IHttpContextAccessor httpContextAccessor, ILogger<NLogHelper> logger)
    {
        _httpContextAccessor = httpContextAccessor;
        _logger = logger;
    }

    private static LogMessage? LogMessage { get; set; }

    public void Log(string message)
    {
        const string mes = "{message}";
        _logger.LogWarning(mes,message);
    }

    public void LogError(Exception ex)
    {
        LogMessage = new LogMessage
        {
            IpAddress = _httpContextAccessor.HttpContext?.Request.Host.Host ?? "NOT FIND",
            LogInfo = ex.InnerException != null ? ex.InnerException.Message : ex.Message,
            StackTrace = ex.StackTrace ?? "NULL",
            OperationTime = DateTime.Now,
            OperationName = "admin"
        };
        var res = LogFormat.ErrorFormat(LogMessage);
        // ReSharper disable once TemplateIsNotCompileTimeConstantProblem
        _logger.LogError(message: res);
    }
}